package controller;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Paper;
import pojo.Question;
import pojo.Student;
import pojo.Teacher;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;
import static java.lang.System.setOut;

@Controller
public class QuestionController {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();

    int q_id = 1;//设置试题编号
    int t_id = 1;

    public QuestionController() throws IOException {
    }

    //跳转到教师试题管理页面
    @RequestMapping("/toQuestionBank")
    public String toQuestionPage() {
        return "questionBank.jsp";
    }

    //跳转到教师查看"生成考试"
    @RequestMapping("/toCreatePaper")
    public String toPaperPage() {
        return "createPaper.jsp";
    }

    //教师查看所有试题列表
    @RequestMapping(value = "/loadQuestionList")
    public String loadQuestionList(HttpSession session) throws Exception {
        List<Question> get_questions = sqlSession.selectList("mapper.Question.getQuestionList");//查询所有试题
        session.setAttribute("get_questions", get_questions);

        return "questionBank.jsp";
    }

    //教师根据名字查询试题列表
    @RequestMapping(value = "/searchQuestionByC_name")
    public String searchQuestionByC_name(Question question, HttpSession session) throws Exception {
        String c_name = question.getC_name();//搜素试题的课程名称
        out.println("查询试题，后端java获取到的试题课程名称：" + question.getC_name());
        List<Question> questionsByC_name = sqlSession.selectList("mapper.Question.searchQuestionByC_name", c_name);//找到包含该名字的试卷
        if (questionsByC_name == null) {//查找为空
            out.println("数据为空");
        } else {
            session.setAttribute("get_questions", questionsByC_name);//设置找到的试卷为session对象
        }
        return "questionBank.jsp";
    }

    //新增试题
    @RequestMapping(value = "/addQuestion",method = RequestMethod.POST)
    public String addQuestion(@RequestParam(value="c_name")String c_name, @RequestParam(value="q_type") String q_type, @RequestParam(value="q_content")String q_content, @RequestParam(value="q_standard")String q_standard, @RequestParam(value="op_A")String op_A, @RequestParam(value="op_B")String op_B, @RequestParam(value="op_C")String op_C, @RequestParam(value="op_D")String op_D, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object mess=null;//用于传递弹窗显示内容
        out.println("addQuestion获取到的c_name:" + c_name);
        Question q = new Question();
        q.setC_name(c_name);
        q.setQ_type(q_type);
        q.setQ_content(q_content);
        q.setQ_standard(q_standard);
        q.setOp_A(op_A);
        q.setOp_B(op_B);
        q.setOp_C(op_C);
        q.setOp_D(op_D);
        int rows = sqlSession.insert("mapper.Question.addQuestion", q);//插入数据
        sqlSession.commit();//提交更新数据库
        if (rows > 0) {
            out.println("成功添加了" + rows + "个试题！\n");
            //重定向到试题页面
            mess = "成功添加了" + rows + "个试题！";
            request.getSession().setAttribute("message", mess);
            response.sendRedirect("loadQuestionList");//返回考题管理并及时更新试题，新添加的试题也会及时显示
        } else {
            out.println("添加失败！\n");
            //重定向到试题页面
            mess = "添加失败！";
            request.getSession().setAttribute("message", mess);
            response.sendRedirect("loadQuestionList");
        }

        return"questionBank.jsp";
    }

    //修改试题信息
    @RequestMapping(value = "/updateQuestion",method = RequestMethod.POST)
    public void updateQuestion(Question question,HttpServletResponse response,HttpServletRequest request) throws IOException {
        Object mess;
        Question q=new Question();
        out.println("更新试题获取到的试题id："+question.getQ_id());
        out.println("更新试题获取到的课程名称："+question.getC_name().trim());//字符串都消除空格
        out.println("更新试题获取到的试题类型："+question.getQ_type());
        q.setQ_id(question.getQ_id());
        q.setC_name(question.getC_name().trim());
        q.setQ_type(question.getQ_type().trim());
        q.setQ_content(question.getQ_content().trim());
        q.setQ_standard(question.getQ_standard().trim());
        q.setOp_A(question.getOp_A().trim());
        q.setOp_B(question.getOp_B().trim());
        q.setOp_C(question.getOp_C().trim());
        q.setOp_D(question.getOp_D().trim());
        int rows=sqlSession.update("mapper.Question.updateQuestion",q);//更新试题
        sqlSession.commit();//提交更新数据库
        if(rows>0){
            out.println("您成功修改了"+rows+"条试题数据！\n");
            mess = "您成功修改了"+rows+"条试题数据！";
            request.getSession().setAttribute("message", mess);
            response.sendRedirect("loadQuestionList");//返回考题管理并及时更新试题
        } else{
            out.println("修改试题失败！\n");
            mess = "修改试题失败！";
            request.getSession().setAttribute("message", mess);
            response.sendRedirect("loadQuestionList");
        }
    }

    //删除试题
    @RequestMapping(value = "/deleteQuestion",method = RequestMethod.POST)
    public void deleteQuestion(@RequestParam(value="delete_id")String delete_id,HttpServletRequest request,HttpServletResponse response) throws IOException {
        Object mess;
        Question q=new Question();
        String q_id1=delete_id.trim();//去除多余空格
        out.println("删除试题获取到的试题id："+Integer.parseInt(q_id1));
        int q_id=Integer.parseInt(q_id1);//转换为int
        q.setQ_id(q_id);
        int rows=sqlSession.delete("mapper.Question.deleteQuestion",q_id);//通过试题id删除对应的试题
        sqlSession.commit();//提交更新数据库
        if(rows>0){
            out.println("您成功删除了"+rows+"条试题数据！\n");
            //重定向到试题页面
            mess = "您成功删除了"+rows+"条试题数据！";
            request.getSession().setAttribute("message", mess);
            response.sendRedirect("loadQuestionList");//返回考题管理并及时更新试题
        } else{
            out.println("删除试题失败！\n");
            mess = "添加失败！";
            request.getSession().setAttribute("message", mess);
            response.sendRedirect("loadQuestionList");
        }
    }

    //新增试卷后添加试题
    @RequestMapping(value = "/QuestionList")
    public String QuestionList(HttpSession session) throws Exception {
        List<Question> get_questions = sqlSession.selectList("mapper.Question.getQuestionList");//获取所有试题信息，用于新增试卷后及时添加试题
        session.setAttribute("get_questions", get_questions);//将获取到的试题列表设置session传递给前端add_paper页面
        Paper paper=(Paper)session.getAttribute("paper");//给新建成的试卷添加试题，createPaper.jsp,add()
        out.println("QuestionList获得的p_id:"+paper.getP_id());
        session.setAttribute("paper",paper);//设置session

        return "add_paper.jsp";
    }

    //给需要修改的试卷添加试题
    @RequestMapping(value = "/QuestionList_add")
    public String QuestionList_add(HttpSession session,@RequestParam(value="p_id")String p_id) throws Exception {
        List<Question> get_questions = sqlSession.selectList("mapper.Question.getQuestionList");
        session.setAttribute("get_questions", get_questions);//将获取到的试题列表设置session传递给前端add_question_to_paper页面
        out.println("QuestionList_add获得的p_id:"+p_id);
        session.setAttribute("p_id",p_id);//给原有试卷添加试题

        return "add_question_to_paper.jsp";
    }

    //教师根据名称搜索题目，用于跳转add_paper.jsp
    @RequestMapping(value = "/searchQuestion")
    public String searchQuestion(Question question, HttpSession session) throws Exception {
        String c_name = question.getC_name().trim();//搜素试题的课程名称
        out.println("add_paper查询试题，后端java获取到的试题课程名称：" + question.getC_name());
        List<Question> questionsByC_name = sqlSession.selectList("mapper.Question.searchQuestionByC_name", c_name);//找到包含该名字的试卷
        if (questionsByC_name == null) {//查找为空
            out.println("数据为空");
        } else {
            session.setAttribute("get_questions", questionsByC_name);//设置找到的试卷为session对象
        }
        return "add_paper.jsp";
    }

    //教师根据名称搜索题目，用于跳转add_question_to_paper.jsp
    @RequestMapping(value = "/searchQuestion_add")
    public String searchQuestion_add(Question question, HttpSession session) throws Exception {
        String c_name = question.getC_name().trim();//搜素试题的课程名称
        out.println("add_question_to_paper查询试题，后端java获取到的试题课程名称：" + question.getC_name());
        List<Question> questionsByC_name = sqlSession.selectList("mapper.Question.searchQuestionByC_name", c_name);//找到包含该名字的试卷
        if (questionsByC_name == null) {//查找为空
            out.println("数据为空");
        } else {
            session.setAttribute("get_questions", questionsByC_name);//设置找到的试卷为session对象
        }
        return "add_question_to_paper.jsp";
    }
}
